# coding=utf-8
from PyQt5.QtWidgets import QAbstractItemView, QListWidget, QListWidgetItem, QGraphicsPixmapItem, QListView
from PyQt5.QtCore import pyqtSignal, Qt, QSize
from PyQt5.QtGui import QFont


class ListWidgetItem(QListWidgetItem):
    imageWindow = None  # 图像工作窗口

    def __init__(self, parent=None):
        super().__init__(parent=None)


class ImageWindowList(QListWidget):
    window = None    # 主窗体
    previous = None     # 前一个选择的列表项目
    # 信号
    currentItemChanged = pyqtSignal(list)  # 选择图层信号
    itemRowChanged = pyqtSignal()   # 选择图像工作窗口改变事件
    # 字体
    font = QFont()
    font.setPointSize(8)

    def __init__(self, window):
        super().__init__(window.imageWindowListPanel)
        self.window = window
        self.Ui_init()
        self.setWindowFlags(Qt.FramelessWindowHint)
        self.updateSize()
        self.show()

    def Ui_init(self):
        # self.setIconSize(QSize(36, 36))
        # 特性
        self.setFlow(QListView.LeftToRight)

    def newWindow(self, imageWindow, info={}):
        """
        create a new imageWindowListItem
        the window will be format by the info of [dic info]
        :param imageWindow:
        :param info:
        info = {
            'title',str
        }
        :return:QListWidgetItem  the new ListItem of the new imageWindow
        """
        newListItem = ListWidgetItem()
        newListItem.setFont(self.font)
        # newListItem.setTextAlignment(Qt.AlignLeft | Qt.AlignVCenter)
        # 名字
        title = info.get('title')
        if title is None:
            title = '未命名 ' + str(self.count())
        newListItem.setText(title)

        self.insertItem(self.count(), newListItem)
        newListItem.show = True
        newListItem.imageWindow = imageWindow
        imageWindow.listItem = newListItem
        # 发射图层顺序改变信号
        self.itemRowChanged.emit()
        return newListItem

    def removeWindow(self, listItem):
        if listItem:
            # 移除列表项目
            print('移除', listItem.text())
            self.takeItem(self.row(listItem))
            self.removeItemWidget(listItem)
            del listItem
            self.itemRowChanged.emit()

    def selectedWindowChangeSlot(self, arg):
        """
        :param arg: [current, previous]
        :return:
        """
        window = self.window  # MainWindow

        previous = arg[1]
        if previous:
            previous.imageWindow.hide()
            window.currentImageWindow = None
        current = arg[0]
        if current:
            current.imageWindow.show()
            window.currentImageWindow = current.imageWindow

    def setSelectedWindow(self, item):
        """
        chose an item, but the previous item will be deselected
        :return:
        """
        if item:
            previous = self.previousItem
            if item != previous:
                item.setSelected(True)
                self.setCurrentItem(item)
                if previous:
                    previous.setSelected(False)
                self.previous = item
                self.selectedWindowChangeSlot([item, previous])

    @property
    def previousItem(self):
        """
        上一次选择的项目
        :return:
        """
        return self.previous

    @property
    def getSelectedItems(self):
        """
        get all items selecter
        :return: QList
        """
        return self.selectedItems()

    @property
    def allItems(self):
        """
        return all QListWidgetItem
        :return: list of QListWidgetItem
        """
        ret = []
        for row in range(0, self.count()):
            ret.append(self.item(row))
        return ret

    def updateSize(self):
        self.resize(self.parent().size())
        self.move(0, 0)
